package com.cwt_taxi_xa.custom_view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cwt_taxi_xa.R;
import com.cwt_taxi_xa.custom_view.MarqueeText.OnScrollEndListener;
import com.cwt_taxi_xa.protocol.DeviceBiz;
import com.cwt_taxi_xa.service.SerialPortService;

public class MyToast{

	private final static String TAG = "MyToast";

	private Context mContext;
	private WindowManager mWm;
	private LayoutParams mParams;
	private View mView;
	private String text;

	/**
	 * 构造
	 * @param context
	 */
	public MyToast(Context context) {
		this.mContext = context;
		initParams();
	}

	public void setText(String text){
		this.text = text;
	}

	/**
	 * 初始化窗体属性
	 */
	private void initParams() {
		try {
			mWm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

			mParams = new WindowManager.LayoutParams();

			mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
			mParams.width = WindowManager.LayoutParams.MATCH_PARENT;
			mParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
					| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
			// 类型
			mParams.type = WindowManager.LayoutParams.TYPE_PHONE;
			//		mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
			//                | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;

			// 透明，不透明会出现重叠效果
			mParams.format = PixelFormat.TRANSLUCENT;
			// 初始化吐司窗口布局
			if(DeviceBiz.getMsgShowType() == 0){
				mView = View.inflate(mContext, R.layout.message_layout, null);
			}else{
				mView = View.inflate(mContext, R.layout.message_layout_silence, null);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 弹出自定义吐司
	 */
	public void show() {
		try {
			LinearLayout layout_root = (LinearLayout) mView.findViewById(R.id.toast_layout_root);
			if(DeviceBiz.getMsgBgSwitcher() == 1){
				// 背景开
				layout_root.setBackgroundColor(Color.parseColor("#C02F3130"));
			}else{
				// 背景关
				layout_root.setBackgroundColor(Color.parseColor("#00000000"));
			}
			if(DeviceBiz.getMsgShowType() == 0){
				MarqueeText tvName = (MarqueeText) mView.findViewById(R.id.toast_text);
				// 设置显示的文字
				tvName.setText(text);
				if(DeviceBiz.getMsgColor() == 1){
					// 绿色
					tvName.setTextColor(Color.parseColor("#00FF00"));
				}else{
					// 白色
					tvName.setTextColor(Color.parseColor("#ffffff"));
				}

				if (mView != null) {
					if (mView.getParent() != null) {
						mWm.removeView(mView);
					}
				}
				// 添加到窗体管理器中才能显示出来
				mWm.addView(mView, mParams);
				tvName.setOnScrollEndListener(new OnScrollEndListener() {

					@Override
					public void stopScroll() {
						// TODO Auto-generated method stub
						hideToast();
					}
				});
				tvName.startScroll();
			}else{
				TextView tvName = (TextView) mView.findViewById(R.id.toast_text);
				// 设置显示的文字
				tvName.setText(text);
				if(DeviceBiz.getMsgColor() == 1){
					// 绿色
					tvName.setTextColor(Color.parseColor("#00FF00"));
				}else{
					// 白色
					tvName.setTextColor(Color.parseColor("#ffffff"));
				}
				if (mView != null) {
					if (mView.getParent() != null) {
						mWm.removeView(mView);
					}
				}
				// 添加到窗体管理器中才能显示出来
				mWm.addView(mView, mParams);
				SerialPortService.handler.postDelayed(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						hideToast();
					}
				}, 8000);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	/**
	 * 从父窗体中移除吐司
	 */
	public void hideToast() {
		try {
			if (mView != null) {
				if (mView.getParent() != null) {
					mWm.removeView(mView);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setGravity(int gravity, int i, int y) {
		// TODO Auto-generated method stub
		// 位置属性
		mParams.gravity = gravity;  // 方位
		mParams.x = i;
		mParams.y = y;
	}
}
